package com.xch.controller;

import com.github.pagehelper.PageInfo;
import com.xch.pojo.Roleresources;
import com.xch.pojo.Userses;
import com.xch.service.MenuService;
import com.xch.service.UserService;
import com.xch.vo.LoginVO;
import com.xch.vo.ResultVO;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private MenuService menuService;

    @RequestMapping("getUserList")
    @ResponseBody
    public ResultVO<Userses> getUserList(Integer pageNumber,Integer pageSize,LoginVO vo){
        if(pageNumber == null || pageNumber<=0){
            pageNumber = 1;
        }
        if(pageSize == null || pageSize<=0){
            pageSize = 5;
        }
        return new ResultVO(userService.getUserList(pageNumber,pageSize,vo));
    }

    @RequestMapping("/edit")
    public ResultVO<Boolean> edit(@PathVariable("id") Integer id){
        System.out.println("------------");
        System.out.println(id);
        System.out.println("------------");
        return new ResultVO<Boolean>();
    }

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    @ResponseBody
    public ResultVO<Userses> login(HttpSession session,LoginVO vo){
        return userService.login(session,vo);
    }

    @RequestMapping(value = "/menu",method = RequestMethod.GET)
    @ResponseBody
    public ResultVO<Integer> getMenu (HttpSession session){
        return menuService.getMenu(session);
    }

    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    @ResponseBody
    public String logout(HttpSession session, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        // 清除session
        session.removeAttribute("user");
        Userses user = (Userses)session.getAttribute("user");
//        model.addAttribute("login",)
        request.getServletContext().getRequestDispatcher("/pages/login.html").forward(request,response);

        return "ok";
    }

    @RequestMapping(value = "reviewPassword",method = RequestMethod.PUT)
    @ResponseBody
    public ResultVO<Boolean> reviewPassword (HttpSession session,LoginVO vo){
        System.out.println(vo);
        System.out.println(session.getAttribute("user"));
        return userService.revisePassword(session, vo);
    }
}
